package com.rich.group.service.impl;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rich.common.constant.MyGlobalConstant;
import com.rich.group.mapper.GroupBuyMapper;
import com.rich.group.pojo.GroupBuy;
import com.rich.group.service.GroupBuyService;
import com.rich.group.vo.GroupBuySearchVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author minicoco
 * @version 1.0
 * @date 2021/8/16 10:37
 */
@Service
@Slf4j
public class GroupBuyServiceImpl implements GroupBuyService {

    @Autowired
    private GroupBuyMapper groupBuyMapper;
    /**
     * redis 序列化配置 保证显示不乱吗
     */
    private RedisTemplate redisTemplate;
    @Autowired(required = false)
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        //Json 序列化配置
        Jackson2JsonRedisSerializer objectJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        objectJackson2JsonRedisSerializer.setObjectMapper(om);

        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(objectJackson2JsonRedisSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(objectJackson2JsonRedisSerializer);
        this.redisTemplate = redisTemplate;
    }


    /**
     * 添加团购活动
     * @param groupBuy
     */
    @Override
    public void addGroupBuy(GroupBuy groupBuy) {
        //赋默认值
        groupBuy.setCreateTime(new Date());
        groupBuy.setStatus(MyGlobalConstant.GroupBuyConstant.NOT_START);
        groupBuy.setStartStatus(MyGlobalConstant.GroupBuyConstant.IS_NORMAL);
        groupBuy.setIsDelete(MyGlobalConstant.GroupBuyConstant.NOT_DELETE);
        groupBuy.setJoinNum(0);
        //TODO 经纪人的id为当前登录人
        groupBuyMapper.addGroupBuy(groupBuy);
    }

    /**
     * 修改团购状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, String status) {
        groupBuyMapper.updateStatus(id,status);
    }

    /**
     * 开团
     * @param groupBuy
     */
    @Override
    public void startGroup(GroupBuy groupBuy) {
        groupBuyMapper.updateById(groupBuy);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void removeGroupBuy(Long id) {
        groupBuyMapper.removeGroupBuy(id);
    }

    /**
     * 经纪人端列表
     * @return
     */
    @Override
    public List<GroupBuy> getBrokerList() {
        return groupBuyMapper.getBrokerList();
    }

    /**
     * 客户端列表
     * @return
     */
    @Override
    public List<GroupBuy> getUserList() {
        return groupBuyMapper.getUserList();
    }

    /**
     * 查询单条
     * @param id
     * @return
     */
    @Override
    public GroupBuy fineOneById(Long id) {
        return groupBuyMapper.fineOneById(id);
    }

    /**
     * 后台管理列表
     * @param groupBuySearchVo
     * @return
     */
    @Override
    public List<GroupBuy> getBackgroundList(GroupBuySearchVo groupBuySearchVo) {
        return groupBuyMapper.getBackgroundList(groupBuySearchVo);
    }

    /**
     * 存入缓存
     */
    @Override
    public void saveCache() {
        //获取用户端的列表
        List<GroupBuy> userList = groupBuyMapper.getUserList();
        //以hash 的范式存入redis中
        //使用parallelStream并行处理加快遍历速度
        userList.parallelStream().forEach(item ->{
            //遍历添加
            redisTemplate.opsForHash()
                    .put(
                            MyGlobalConstant.GroupBuyConstant.GROUP_BUY,
                            MyGlobalConstant.GroupBuyConstant.GROUP_BUY_ITEM+item.getId(),
                            item
                    );
        });
        log.info("存入缓存中....");
    }

}
